import $ from 'jquery';
import {message} from 'antd';
import { customCookie } from "../Util.js";

export var HttpClient = (function () {
    //请求类型【truth,mock】
    // var REQUEST = "mock";
    var REQUEST = "truth";

    var requestSuccess = 0;
    var requestDataError = 1;
    var requestServiceError = 2;

    //获取请求host
    var httpClientHost = '';
    switch (process.env.NODE_ENV) {
        case "development":
            //开发环境
            httpClientHost = 'http://192.168.199.160:3001/';
            break;
        case "production":
            httpClientHost = 'http://192.168.199.160:3001/';
            break;
        default:
            break;
    }
    function query(url, rtype, data, callback) {
        let httpUrl = '';
        if (url.indexOf('http') > -1) {
            httpUrl = url;
            // 中间件接口，增加几个公共参数
        } else {
            httpUrl = httpClientHost + url
        }
            $(function () {
                $.ajax({
                    "url": httpUrl,
                    "method": rtype,
                    "cache" : true,
                    "async":true,
                    "data": data,
                    success: function (d) {
                        callback(d, requestSuccess);
                        // if (d.success) {
                        //      console.log(REQUEST + "--" + url + "--Success:", d);
                        // } else {
                        //         message.error(d);
                        //         callback(d, requestDataError);

                        // }
                    },
                    error: function (e) {
                        //服务异常
                        console.error(REQUEST + "--" + url + "--Error:", e);
                        //message.error("服务器异常！");
                        callback(e, requestServiceError);
                    }
                });
            })
    }

    return {
        ClientHost: httpClientHost,
        GET: 'GET',
        POST: "POST",
        PUT: "PUT",
        DELETE: "DELETE",
        requestSuccess: requestSuccess,
        requestDataError: requestDataError,
        requestServiceError: requestServiceError,
        query: query,
        REQUEST: REQUEST
    }


}());
